package gs.android.videopokercollection;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class GambleActivity extends Activity{
	private boolean[] dealed;
	private Button diamonds, hearts, spades, clubs, black, red, collect;
	private TypedArray images;
	private ImageView img;
	private TextView win;
	private TimerTask timerTask;
	private Timer timer;
	private int prize;
	
	@Override
	public void onCreate(Bundle savedInstance){
		super.onCreate(savedInstance);
		setContentView(R.layout.gamble);
		
		WindowManager.LayoutParams brightness = getWindow().getAttributes();
		brightness.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        getWindow().setAttributes(brightness);
        
		prize = getIntent().getExtras().getInt("prize");
		win = (TextView) findViewById(R.id.lblWin);
		win.setText("" + prize);

        images = getResources().obtainTypedArray(R.array.images);
        img = (ImageView)findViewById(R.id.imgCard);
        dealed = new boolean[52];
		for (int i = 0; i < 52; i++){
			dealed[i] = false;
		}
		
		diamonds = (Button)findViewById(R.id.btnDiamonds);
		diamonds.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				suits(3);
			}
		});
		
		hearts = (Button)findViewById(R.id.btnHearts);
		hearts.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				suits(1);
			}
		});
		
		spades = (Button)findViewById(R.id.btnSpades);
		spades.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				suits(0);
			}
		});
		
		clubs = (Button)findViewById(R.id.btnClubs);
		clubs.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				suits(2);
			}
		});
		
		black = (Button)findViewById(R.id.btnBlack);
		black.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				colour(0, 2);
			}
		});
		
		red = (Button)findViewById(R.id.btnRed);
		red.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				colour(1, 3);
			}
		});
		
		collect = (Button) findViewById(R.id.btnCollectGamblings);
		collect.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = getIntent();
				intent.putExtra("gamblePrize", prize);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
	
	private void suits(int n){
		int card = (int)((deal() - 1) / 13);
		if (card == n) {
			prize *= 4;
			win.setText("" + prize);
		} else {
			prize = 0;
			win.setText("" + prize);
			collect.setEnabled(false);
			lost();
		}
	}
	
	private void colour(int n, int m){
		int card = (int)((deal() - 1) / 13);
		if (card == n || card == m) {
			prize *= 2;
			win.setText("" + prize);
		} else {
			prize = 0;
			win.setText("" + prize);
			collect.setEnabled(false);
			lost();
		}
	}
	
	private int deal(){
		int n = (int)(Math.random() * 52) + 1;
		while (dealed[n - 1]){
			n = (int)(Math.random() * 52) + 1;
		}
		dealed[n - 1] = true;
		img.setImageResource(images.peekValue(n-1).resourceId);
		
		return n;
	}
	
	private void lost() {
		diamonds.setClickable(false);
		hearts.setClickable(false);
		spades.setClickable(false);
		clubs.setClickable(false);
		black.setClickable(false);
		red.setClickable(false);
		collect.setClickable(false);
		
		timer = new Timer("cards");
        timerTask = new TimerTask() {
			@Override
			public void run() {
				exit();
			}
		};
        timer.schedule(timerTask, 1000);
	}
	
	private void exit() {
    	runOnUiThread(new Runnable() {
			@Override
			public void run() {
				timerTask.cancel();
				timer.cancel();
				Intent intent = getIntent();
				intent.putExtra("gamblePrize", 0);
				setResult(RESULT_OK, intent);
				finish();
			}
    	});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
